//
//  ViewController.m
//  DGCircleDemo
//
//  Created by HunDegong on 2024/12/5.
//

#import "ViewController.h"
#import "HDGCustomCircleView.h"
#import "HDGCustomSlider.h"

@interface ViewController ()

@property (nonatomic, strong) HDGCustomCircleView *circleView;

@property (nonatomic, assign) CGPoint gestureStartPoint;

@property (nonatomic, strong) HDGCustomSlider *slider;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建并初始化自定义视图
    self.circleView = [[HDGCustomCircleView alloc] initWithFrame:self.view.bounds];
    self.circleView.backgroundColor = [UIColor colorWithRed:16.0/255.0 green:18.0/255.0 blue:50.0/255.0 alpha:1.0];
    [self.view addSubview:self.circleView];
    
    CGFloat sliderWidth = 200;
    CGFloat sliderHeight = [HDGCustomSlider defaultHeight];
    CGFloat sliderX = (UIScreen.mainScreen.bounds.size.width - sliderWidth) * 0.5;
    CGFloat sliderY = (UIScreen.mainScreen.bounds.size.height - sliderHeight) * 0.5 + 150.0;
    self.slider = [[HDGCustomSlider alloc] initWithFrame:CGRectMake(sliderX, sliderY, sliderWidth, sliderHeight)];
    __weak typeof(self) weakSelf = self;
    [self.slider dragSliderHandler:^(float minValue, float maxValue) {
        NSLog(@"drag slider handler minValue: %lf maxValue: %lf", minValue, maxValue);
        // 设置 min 和 max 值
        weakSelf.circleView.minValue = minValue;
        weakSelf.circleView.maxValue = maxValue;
    }];
    [self.view addSubview:self.slider];
    
    // 设置初始位置
    [self.circleView setCursorPositionWithX:0 Y:0];
    [self.circleView setSecondCursorPositionWithX:0 Y:0];
    
    // 添加手势识别
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self.view addGestureRecognizer:panGesture];
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture {
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            self.gestureStartPoint = [gesture locationInView:self.view];
            break;
            
        case UIGestureRecognizerStateChanged:
        {
            CGPoint currentPoint = [gesture locationInView:self.view];
            CGPoint translation = CGPointMake(currentPoint.x - self.gestureStartPoint.x, currentPoint.y - self.gestureStartPoint.y);
            [self.circleView setCursorPositionWithX:translation.x Y:translation.y];
            [self.circleView setSecondCursorPositionWithX:translation.x - 10 Y:translation.y - 10];
        }
            break;
            
        case UIGestureRecognizerStateEnded:
            // 可以在这里处理手势结束后的操作
            self.gestureStartPoint = CGPointZero;
            break;
            
        default:
            break;
    }
}

@end
